package com.metaera.app.ui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.metaera.app.LocalConfigStore;
import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;
import com.metaera.app.controller.presenter.NoticeListPresenter;
import com.metaera.app.controller.presenter.UserInfoPresenter;
import com.metaera.app.controller.view.MineNoticeView;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.model.AppConfigResponse;
import com.metaera.app.data.model.NoticeListResponse;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.ui.activities.AboutActivity;
import com.metaera.app.ui.activities.CollectionListActivity;
import com.metaera.app.ui.activities.FeedbackActivity;
import com.metaera.app.ui.activities.HistoryListActivity;
import com.metaera.app.ui.activities.InteractListActivity;
import com.metaera.app.ui.activities.MessageCenterActivity;
import com.metaera.app.ui.activities.SettingActivity;
import com.metaera.app.ui.activities.SocialMediaActivity;
import com.metaera.app.ui.activities.UserEditCenterActivity;
import com.metaera.app.ui.activities.UserProfileActivity;
import com.metaera.app.ui.activities.WebViewActivity;
import com.metaera.app.ui.core.BaseFragment;
import com.metaera.app.utils.DataCleanManager;
import com.metaera.app.utils.LoginClickListener;
import com.metaera.app.utils.ToastUtils;
import com.metaera.app.utils.ViewUtils;
import com.metaera.app.utils.event.LogoutEvent;
import com.metaera.app.utils.event.UserEditEvent;
import com.metaera.app.utils.image.GlideApp;

import org.greenrobot.eventbus.Subscribe;

public class MineFragment extends BaseFragment implements View.OnClickListener {

    private TextView tvUUID, tvName;
    private ImageView ivAvatar, btn_me_edit;
    private UserInfoPresenter mUserPresenter;

    @Override
    protected int getLayoutId() {

        return R.layout.fragment_me;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        ViewUtils.addStatusBarHeightPadding(findViewById(R.id.fragment_me_info_nologin));

        // find
        ViewGroup layoutNotice = findViewById(R.id.layout_notice);
        tvUUID = findViewById(R.id.tv_uuid);
        tvName = findViewById(R.id.fragment_me_name_textview);
        ivAvatar = findViewById(R.id.fragment_me_head_imageview);
        btn_me_edit = findViewById(R.id.btn_me_edit);

        // init view
        if (LocalConfigStore.getInstance().isLogin()) {
            renderUser(LocalConfigStore.getInstance().getUser());

        } else {
            renderNoLogin(null);
        }

        // listener
        findViewById(R.id.tv_clear_cache).setOnClickListener(this);
        findViewById(R.id.layout_mine_setting).setOnClickListener(this); // 设置
        findViewById(R.id.layout_mine_feedback)
                .setOnClickListener(new LoginClickListener(this)); // 意见反馈
        findViewById(R.id.fragment_me_info_nologin) // 登录进入个人主页
                .setOnClickListener(new LoginClickListener(this));
        findViewById(R.id.layout_mine_history) // 最近浏览
                .setOnClickListener(new LoginClickListener(this));
        findViewById(R.id.layout_collections) // 我的收藏
                .setOnClickListener(new LoginClickListener(this));
        findViewById(R.id.layout_interact).setOnClickListener( // 我的互动
                new LoginClickListener(v -> startActivity(InteractListActivity.class)));
        btn_me_edit.setOnClickListener(this); // 编辑个人信息
        findViewById(R.id.layout_mine_msg) // 消息中心
                .setOnClickListener(v -> startActivity(MessageCenterActivity.class));
        findViewById(R.id.layout_social_media) // 社交媒体
                .setOnClickListener(v -> startActivity(SocialMediaActivity.class));

        findViewById(R.id.layout_about).setOnClickListener(v -> // 关于
                startActivity(AboutActivity.newInstance(v.getContext(), AboutActivity.ABOUT_TYPE)));

        findViewById(R.id.layout_business).setOnClickListener(v -> // 商务合作
                startActivity(AboutActivity.newInstance(v.getContext(), AboutActivity.BUSINESS_TYPE)));

        findViewById(R.id.layout_content).setOnClickListener(v -> // 内容合作
                startActivity(AboutActivity.newInstance(v.getContext(), AboutActivity.CONTENT_TYPE)));

        // 一些配置
        AppConfigResponse config = LocalConfigStore.getInstance().getConfig();
        if (config != null) {
            findViewById(R.id.layout_disclaimer).setOnClickListener(v -> // 免责声明
                    startActivity(WebViewActivity.newIntent(v.getContext(), config.getDisclaimers().getValue())));
            findViewById(R.id.layout_terms).setOnClickListener(v -> // 服务条款
                    startActivity(WebViewActivity.newIntent(v.getContext(), config.getTerm_of_service().getValue())));
        }

        // network 通知列表
        IPaginationPresenter<IPaginationView<NoticeListResponse>, NoticeListResponse>
                presenter = new NoticeListPresenter();
        presenter.setModelAndView(new MineNoticeView(layoutNotice));
        presenter.requestRefresh();

        // 用户登录
        mUserPresenter = new UserInfoPresenter();
        mUserPresenter.setModelAndView(data -> {
            renderUser(data);
            LocalConfigStore.getInstance().updateUser(data);
        });
        mUserPresenter.execute();
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.layout_mine_setting) {
            startActivity(SettingActivity.class); // 我的设置
//            startActivity(UserProfileActivity.class);

        } else if (view.getId() == R.id.layout_mine_feedback) { // 意见反馈
            startActivity(new Intent(getContext(), FeedbackActivity.class));

        } else if (view.getId() == R.id.fragment_me_info_nologin) {
            UserDetailsResponse user = LocalConfigStore.getInstance().getUser();
            startActivity(UserProfileActivity.newIntent(view.getContext(), user.getId()));

        } else if (view.getId() == R.id.btn_me_edit) {
            startActivity(new Intent(getContext(), UserEditCenterActivity.class));

        } else if (view.getId() == R.id.layout_mine_history) { // 最近浏览
            startActivity(HistoryListActivity.class);

        } else if (view.getId() == R.id.layout_collections) { // 我的收藏
            startActivity(CollectionListActivity.class);

        } else if (view.getId() == R.id.tv_clear_cache){
            DataCleanManager.clearAllCache(view.getContext());
            ToastUtils.makeText(R.string.clear_cache_success);
        }
    }

    @Subscribe
    public void renderUser(UserDetailsResponse user) {
        ViewUtils.setVisibility(View.VISIBLE, btn_me_edit, tvUUID);
        tvUUID.setText("uid:" + user.getId());
        tvName.setText(user.getNickname());

        GlideApp.with(this)
                .load(user.getAvatar_img())
                .avatar()
                .into(ivAvatar);
    }

    @Subscribe
    public void renderNoLogin(LogoutEvent logout) {
        tvName.setText(R.string.click_login);
        ivAvatar.setImageResource(R.drawable.b_ic_avatar_placeholder);
        ViewUtils.setVisibility(View.GONE, btn_me_edit, tvUUID);
    }

    @Subscribe
    public void updateUserInfo(UserEditEvent event){
        mUserPresenter.execute();
    }

}
